package com.example.home.volunteer_service.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.home.R;
import com.example.home.tool.GeneralClass;
import com.example.home.tool.OKHttpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;


public class ServiceMainFragment extends Fragment {

    private ImageView back;
    private TabLayout tabList;
    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_service_main, container, false);

        initView();

        getTabList();


        return view;
    }

    private void getTabList() {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("已完成");
        arrayList.add("已报名");
        arrayList.add("已取消");

        for (String category : arrayList) {
            TabLayout.Tab tab = tabList.newTab();
            tab.setText(category);
            tabList.addTab(tab);
        }

        tabList.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Searchhousing();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    private void Searchhousing() {
        String url = "prod-api/api/volunteer-service/activity/my-list/1?pageNum=1&pageSize=10";

        OKHttpTool.doNetRequest(url, OKHttpTool.GET, GeneralClass.class, new OKHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                GeneralClass generalClass = (GeneralClass) obj;
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (generalClass.getTotal() == 0){
                            Toast.makeText(getActivity(),"暂无记录",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }

            @Override
            public void onFailure(String msg) {
                // 处理请求失败情况
            }
        });
    }

    private void initView() {
        back = view.findViewById(R.id.back);
        tabList = view.findViewById(R.id.tab_list);
    }

}